package com.forlove.app.qselect.school.presenter;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.forlove.app.common.base.mvp.presenter.IBasePresenter;
import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.utils.LogUtil;
import com.forlove.app.common.utils.ParseHelper;
import com.forlove.app.common.utils.StringUtils;
import com.forlove.app.manager.DataManager;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.qselect.CommonEntity;
import com.forlove.app.qselect.login.entity.SchoolDto;
import com.forlove.app.qselect.login.entity.SchoolInfoEntity;
import com.forlove.app.qselect.school.service.ISearchSchoolService;
import com.forlove.app.qselect.school.service.impl.SearchSchoolServiceImpl;
import com.forlove.app.qselect.school.view.SearchSchoolView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yjl on 2017/12/26.
 */

public class SearchSchoolPresenter extends IBasePresenter {
    private SearchSchoolView searchSchoolView;
    private ISearchSchoolService searchSchoolService;

    public SearchSchoolPresenter(SearchSchoolView searchSchoolView) {
        this.searchSchoolView = searchSchoolView;
        if (searchSchoolService == null) {
            searchSchoolService = new SearchSchoolServiceImpl();
        }
    }

    public void loadSchoolInfo() {
        searchSchoolService.loadData(ServerManager.ACTION_GET_SCHOOL, new HashMap(), new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "学校信息:" + result);
                String school = ParseHelper.getString(result, "data");
                if (StringUtils.isNotEmpty(school)) {
                    SchoolDto schoolDto = JSON.parseObject(school, SchoolDto.class);
                    if (schoolDto != null) {
                        DataManager.saveSchoolInfo(school);
                    }
                    searchSchoolView.updateSchoolInfo(schoolDto.getData());
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    searchSchoolView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else {
                    searchSchoolView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                }
            }
        });
    }

    public void loadNearbySchool(Map param) {
        searchSchoolService.loadData(ServerManager.ACTION_SEARCH_SCHOOL, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                List<SchoolInfoEntity> schoolInfoEntities = ParseHelper.getModelList(result, "data.list", SchoolInfoEntity.class);
                searchSchoolView.updateNearbySchool(schoolInfoEntities);
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    searchSchoolView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else {
                    searchSchoolView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                }
            }
        });
    }
}
